/**************************************************************************************************
Filename:       nb_u8g2api.c
Editor:         Tome @ newbit
Revised:        $Date: 2017/5/12 11:20:02 +0800  $
Revision:       $Revision: 00001 $

Description:    
History:        
Notes:          share on github.com/newbitstudio
				support on bbs.newbitstudio.com

**************************************************************************************************/



/**************************************************************************************************
// INCLUDES
**************************************************************************************************/


#include "U8x8lib.h"
#include "u8g2.h"

#include "hal_mcu.h"

#include "hal_oled_u8g2.h"

#include "nb_u8g2api.h"



/**************************************************************************************************
// TYPEDEF
**************************************************************************************************/



/**************************************************************************************************
// CONSTANTS
**************************************************************************************************/

#define NB_PIN_ASSUME_CS    0

/**************************************************************************************************
// LOCAL VERIABLE
**************************************************************************************************/
static u8g2_t u8g2;
//static  u8x8_char_cb cpp_next_cb;
//static u8g2_uint_t tx, ty;

uint8_t flip_color = 0;
uint8_t draw_color = 1;

/**************************************************************************************************
// FUNCTIONS DECLERATION
**************************************************************************************************/

void nbio_out_set(uint8 i);
void nbio_out_clr(uint8 i);
uint8 nbio_in_isset(uint8 i);

void u8x8_SetPin_3Wire_HW_SPI(u8x8_t *u8x8, uint8_t cs, uint8_t reset);
uint8_t u8x8_gpio_and_delay_newbit(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, U8X8_UNUSED void *arg_ptr);
uint8_t u8x8_byte_newbit_hw_3wire_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);

void u8g2_prepare(void);
void u8g2_debug(void);

/**************************************************************************************************
// FUNCTIONS
**************************************************************************************************/

void u8g2_init(const u8g2_cb_t *rotation)
{
	/*
	cpp_next_cb = u8x8_ascii_next;
	tx = 0; ty = 0;
	u8x8_utf8_init(u8g2_GetU8x8(&u8g2));
	
	u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, rotation, u8x8_byte_newbit_hw_3wire_spi, u8x8_gpio_and_delay_newbit  );
		
	u8x8_SetPin_3Wire_HW_SPI(u8g2_GetU8x8(&u8g2), cs, reset);
	*/
	

	u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, rotation, u8x8_byte_newbit_hw_3wire_spi, u8x8_gpio_and_delay_newbit  );
        //u8g2_Setup_ssd1306_128x64_noname_1(&u8g2, rotation, u8x8_byte_newbit_hw_3wire_spi, u8x8_gpio_and_delay_newbit  );
#ifdef U8X8_USE_PINS
	u8x8_SetPin_3Wire_HW_SPI(u8g2_GetU8x8(&u8g2), NB_PIN_ASSUME_CS, 0xFF);
#endif
	u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
	//u8g2_ClearDisplay(&u8g2);
	u8g2_SetPowerSave(&u8g2, 0); // wake up display
	
	
	//u8g2_SetDrawColor( &u8g2, flip_color );
	
	//u8g2_DrawBox(&u8g2,  0, 0,5, 5 );
	//hal_oled_spi_init();
	//Hal_oled_write_string(0,0,"abc 123           ", 0);
	
	//while(1);
	u8g2_prepare();
        U8G2_ClearDisplay();
	//u8g2_debug();
}

u8g2_t* u8g2_instance_get(void)
{
	return &u8g2;
}


void u8g2_prepare(void)
{
	u8g2_SetFont(&u8g2,u8g2_font_6x10_tf);
	//u8g2_SetFont(&u8g2,u8g2_font_ncenB14_tr);	
	
  //u8g2.setFontRefHeightExtendedText();
	//u8g2_SetFontRefHeightExtendedText(&u8g2);
  //u8g2.setDrawColor(1);
	//u8g2_SetDrawColor(&u8g2, 1);
  //u8g2.setFontPosTop();
	u8g2_SetFontPosTop(&u8g2);
  //u8g2.setFontDirection(0);	
	//u8g2_SetFontDirection(&u8g2, 0);	
}

void u8g2_box_frame(uint8_t a) {
  u8g2_DrawStr( &u8g2, 0, 0, "drawBox");
  u8g2_DrawBox(&u8g2,5,10,20,10);
  u8g2_DrawBox(&u8g2,10+a,15,30,7);
  u8g2_DrawStr(&u8g2, 0, 30, "drawFrame");
  u8g2_DrawFrame(&u8g2,5,10+30,20,10);
  u8g2_DrawFrame(&u8g2,10+a,15+30,30,7);
}

void u8g2_debug(void)
{
	//u8g2_SetDrawColor(&u8g2, draw_color);
	//u8g2_SetFont(&u8g2, u8g2_font_6x10_tf);
	
	//u8g2_SetFont(&u8g2,u8g2_font_ncenB14_tr);
  //u8g2_DrawStr(&u8g2, 0,15,"Hello World!");

	
	uint16_t num = 1;
	
	while(1)
	{
		num++;
		if (num > 7)
			num = 0;

                U8G2_ClearBuffer();
		///u8g2_FirstPage(&u8g2);
		
		//do
		//{
			//sprintf(str,"t:%d",num);
			
			
			//u8g2_SetFont(&u8g2,u8g2_font_ncenB14_tr);
			//u8g2_DrawStr(&u8g2, 0,2, str);
			//u8g2_DrawStr(&u8g2,0, 0, "cocoRuler u8");
			//u8g2_DrawBox(&u8g2, 4, 0, 5,5);
			u8g2_box_frame(num & 0x07 );
                        //u8g2_DrawStr(&u8g2, 0,15,"Hello World!");
			
		
		//}while(u8g2_NextPage(&u8g2));
                        
                        U8G2_SendBuffer();
                        
			halMcuWaitMs(100);
	}	
}



uint8_t u8x8_gpio_and_delay_newbit(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, U8X8_UNUSED void *arg_ptr)
{
	uint8_t i;
	
	switch(msg)
	{
		case U8X8_MSG_GPIO_AND_DELAY_INIT:
			//hal_oled_spi_init();
			
#ifdef U8X8_USE_PINS
		for( i = 0; i < U8X8_PIN_CNT; i++ )
		{
			if ( u8x8->pins[i] != U8X8_PIN_NONE )
				{
					if ( i < U8X8_PIN_OUTPUT_CNT )
					{
						// output pins
						//nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(true);
						//nrf_drv_gpiote_out_init(u8x8->pins[i], &out_config);
					}
					else
					{
						// input pins
						//nrf_drv_gpiote_in_config_t in_config = 
							//GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
						//nrf_drv_gpiote_in_init (i, &in_config, NULL);		
					}
				}
			}
		
#endif
		break;
	
	case U8X8_MSG_DELAY_10MICRO:
		/* not used at the moment */
	break;
	
    case U8X8_MSG_DELAY_100NANO:
      /* not used at the moment */
      break;
   
    case U8X8_MSG_DELAY_MILLI:
      /* not used at the moment */
      break;
	
	case U8X8_MSG_DELAY_I2C:
		/* not used at the moment */
	break;
	
	case U8X8_MSG_GPIO_I2C_CLOCK:
    case U8X8_MSG_GPIO_I2C_DATA:
		/* not used at the moment */
	break;
	
  default:
      
#ifdef U8X8_USE_PINS
  if ( msg >= U8X8_MSG_GPIO(0) )
  {
    i = u8x8_GetPinValue(u8x8, msg);
    if ( i != U8X8_PIN_NONE )
    {
      if ( u8x8_GetPinIndex(u8x8, msg) < U8X8_PIN_OUTPUT_CNT ) 
      {
	    if ( arg_int )
                              nbio_out_set(i);
			else
				nbio_out_clr(i);//nrf_drv_gpiote_out_clear(i);
			}
      else
      {
				//u8x8_SetGPIOResult(u8x8, nrf_drv_gpiote_in_is_set(i) == 1 ? 1: 0);
        u8x8_SetGPIOResult(u8x8, nbio_in_isset(i) == 1 ? 1: 0);
			}
    }
	break;
  }
#endif
  return 0;
}
return 1;	
}



uint8_t u8x8_byte_newbit_hw_3wire_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
  uint8_t *data;
  //uint8_t internal_spi_mode;
  static uint8_t bit9 = 0;	// this 
	

  switch(msg)
  {
    case U8X8_MSG_BYTE_SEND:
		
      data = (uint8_t *)arg_ptr;
      while( arg_int > 0 )
      {
#ifdef U8X8_USE_PINS
        u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_enable_level);  	
#else
        OLED_SPI_CS_LOW();
#endif
		//OLED_SPI_CS_LOW();
		hal_oled_write_byte(bit9, *data);		  
		data++;
		arg_int--;
#ifdef U8X8_USE_PINS
        u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);  	
#else
        OLED_SPI_CS_HIGH();
#endif                
		  
      }
      break;
    case U8X8_MSG_BYTE_INIT:
      /* disable chipselect */
#ifdef U8X8_USE_PINS
      u8x8_gpio_SetCS(u8x8, u8x8->display_info->chip_disable_level);
#else
      OLED_SPI_CS_HIGH();
#endif
      /* no wait required here */
      
      /* for SPI: setup correct level of the clock signal */
      // removed, use SPI.begin() instead: pinMode(11, OUTPUT);
      // removed, use SPI.begin() instead: pinMode(13, OUTPUT);
      // removed, use SPI.begin() instead: digitalWrite(13, u8x8_GetSPIClockPhase(u8x8));
      
      /* setup hardware with SPI.begin() instead of previous digitalWrite() and pinMode() calls */
      
	  //SPI.begin();	
	
		hal_oled_spi_init();

      break;
	

    case U8X8_MSG_BYTE_SET_DC:
      //      u8x8_gpio_SetDC(u8x8, arg_int);
      //bit9 = arg_int ; // DC_TYPE_DATA or DC_TYPE_CMD
	
		// u8g2 lib, arg_int = 1(CMD), arg_int = 0 ( DATA)
	  bit9 = (arg_int == 0) ? DC_TYPE_CMD :DC_TYPE_DATA;
		
      break;	
	
	
	default:
		
	break;
	
	}
	return 1;
}



void u8x8_SetPin_3Wire_HW_SPI(u8x8_t *u8x8, uint8_t cs, uint8_t reset)
{
#ifdef U8X8_USE_PINS   
  u8x8_SetPin(u8x8, U8X8_PIN_CS, cs);
  //u8x8_SetPin(u8x8, U8X8_PIN_RESET, reset);
#endif
}


/**************************************************************************************************
* @fn      nbio_out_set
*
* @brief   -
*          -
*
* @param   uint8 i
*          -
*          -
*
* @return  void
**************************************************************************************************/
void nbio_out_set(uint8 i)
{
  if ( i == NB_PIN_ASSUME_CS ) 
    OLED_SPI_CS_HIGH();
}


/**************************************************************************************************
* @fn      nbio_out_clr
*
* @brief   -
*          -
*
* @param   uint8 i
*          -
*          -
*
* @return  void
**************************************************************************************************/
void nbio_out_clr(uint8 i)
{
  if ( i == NB_PIN_ASSUME_CS ) 
    OLED_SPI_CS_LOW();
}


/**************************************************************************************************
* @fn      nbio_in_isset
*
* @brief   -
*          -
*
* @param   uint8 i
*          -
*          -
*
* @return  uint8
**************************************************************************************************/
uint8 nbio_in_isset(uint8 i)
{
  if ( i == NB_PIN_ASSUME_CS)
    return (P1_3 == 1);
  
  return 0;
}






/**************************************************************************************************
Copyright 2016 Newbit Studio. All rights reserved.
**************************************************************************************************/

